package com.example.my_objectives;

import com.example.my_objectives.controller.MainController;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
	ListView			list;
	MainController	_controller;

	private void initializeView() {
		// populate the ListView

		ObjectiveList adapter = new ObjectiveList(MainActivity.this, _controller.getAllObjectiveStatus());
		list = (ListView) findViewById(R.id.lvObjectives);

		list.setAdapter(adapter);
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(MainActivity.this, "Clicked on an objective", Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		_controller = new MainController(this);
		_controller.openDB();

		initializeView();
	}

	@Override
	protected void onPause() {
		super.onPause();
		_controller.closeDB();
		notify("onPause");
	}

	@Override
	protected void onResume() {
		super.onResume();
		_controller.openDB();
		notify("onResume");
	}

	@Override
	protected void onStop() {
		super.onStop();
		_controller.closeDB();
		notify("onStop");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		_controller.closeDB();
		notify("onDestroy");
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		_controller.openDB();
		notify("onRestoreInstanceState");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		_controller.closeDB();
		notify("onSaveInstanceState");
	}

	private void notify(String methodName) {
		String name = this.getClass().getName();
		String[] strings = name.split("\\.");
		Notification noti = new Notification.Builder(this)
				.setContentTitle(methodName + " " + strings[strings.length - 1]).setAutoCancel(true)
				.setSmallIcon(R.drawable.ic_launcher).setContentText(name).build();
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.notify((int) System.currentTimeMillis(), noti);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
			case R.id.action_settings:
				Toast.makeText(MainActivity.this, "Clicked on " + item.getItemId(), Toast.LENGTH_SHORT).show();
				return true;
			case R.id.action_new_objective:
				Intent intent = new Intent(this, NewObjectiveActivity.class);
				startActivity(intent);
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

}
